package com.demo.dao.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class AnnotatedLogging {

    @Pointcut("execution(* com.demo.dao.enity.Student.getName(..))")
    private void getName() {
    }

    @Pointcut("execution(* com.demo.dao.enity.Student.getAge(..))")
    private void getAge() {
    }

    @Pointcut("execution(* com.demo.dao.enity.Student.*(..))")
    private void selectedAll() {
    }

    @Before("getName()")
    public void doBeforeTask() {
    }

    @After("getAge()")
    public void doAfterTask() {
    }

    @AfterReturning(pointcut = "getName()", returning = "retVal")
    public void doAfterReturnningTask(Object retVal) {
        System.out.println("AnnotatedLogging-->Returning:" + retVal.toString());
    }

    @AfterThrowing(pointcut = "selectedAll()", throwing = "ex")
    public void doAfterThrowingTask(Exception ex) {
        System.out.println("AnnotatedLogging-->There has been an exception: " + ex.toString());
    }

    @Around("getAge()")
    public void doAroundTask(ProceedingJoinPoint joinPoint) throws Throwable {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        joinPoint.proceed();
        System.out.println("AnnotatedLogging-->Student profile around setup.");
    }
}
